package com.msconfig.mvp.view.fragment;

/*
 * author: caojunhao
 * date  : 2020/5/11
 * desc  :
 */

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.geo.msconfig.R;
import com.msconfig.bean.dialogBean.OperatorBean;
import com.msconfig.event.EditOperateInfoEvent;
import com.msconfig.event.GprsAddOperatorSuccessEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class SetupGprsAddOperatorFragment extends BaseMvpFragment implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private EditText etOperator;
    private EditText etName;
    private EditText etUser;
    private EditText etPass;
    private Button btnDetermine;
    private String strOperator;
    private String strName;
    private String strUser;
    private String strPass;
    private OperatorBean operatorBean;
    private ArrayList<OperatorBean> list;
    private int position=-1;

    //显示编辑页过来的信息
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showEditInfo(EditOperateInfoEvent event) {
        position=event.getPosition();
        if(!event.isEdit())
            return;
        etOperator.setText(event.getStrOperator());
        etName.setText(event.getStrName().substring(3));
        etUser.setText(event.getStrUser().substring(3));
        etPass.setText(event.getStrPass().substring(3));
    }

    public static SetupGprsAddOperatorFragment newInstance() {
        Bundle args = new Bundle();
        SetupGprsAddOperatorFragment fragment = new SetupGprsAddOperatorFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        if(v==btnDetermine){
            onClickDetermine();
        }if(v==ivCommonBack){
            backTo();
        }
    }

    private void onClickDetermine() {
        strOperator=etOperator.getText().toString().trim();
        strName=etName.getText().toString().trim();
        strUser=etUser.getText().toString().trim();
        strPass=etPass.getText().toString().trim();
        if(!strOperator.equals("")){
            list=new ArrayList<>();
            operatorBean=new OperatorBean();
            operatorBean.setOperator(strOperator);
            operatorBean.setName(getString(R.string.operator_name, strName));
            operatorBean.setUser(getString(R.string.operator_user, strUser));
            operatorBean.setPass(getString(R.string.operator_pass, strPass));
            EventBus.getDefault().post(new GprsAddOperatorSuccessEvent(operatorBean,position));
            list.add(operatorBean);
            backTo();
        }else{
            Toast.makeText(getContext(),"请输入相关信息！", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_setup_gprs_add_operator;
    }

    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        etOperator = (EditText)view.findViewById( R.id.et_operator );
        etName = (EditText)view.findViewById( R.id.et_name );
        etUser = (EditText)view.findViewById( R.id.et_user );
        etPass = (EditText)view.findViewById( R.id.et_pass );
        btnDetermine = (Button)view.findViewById( R.id.btn_determine);
    }

    @Override
    protected void initListener(View view) {

        btnDetermine.setOnClickListener(this);
        ivCommonBack.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        tvCommonTitle.setText("添加运营商");
    }

}
